﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;

namespace RI
{
    public class Business : IDisposable
    {

        #region 新闻基本操作
        /// <summary>
        /// 新闻数据结构
        /// </summary>
        public struct News
        {
            public string cat;
            public string title;
            public string html;            
            public string ubb;
        }

        /// <summary>
        /// 获取全部新闻
        /// </summary>
        /// <returns></returns>
        public DataTable GetNews()
        {
            string get = "SELECT * FROM View_News";
            using (DBase db = new DBase())
            {
                return db.GetDataTable(get);
            }
        }

        /// <summary>
        /// 获取指定分类新闻
        /// </summary>
        /// <param name="itemCat">分类名称</param>
        /// <returns>查询结果</returns>
        public DataTable GetItemCatNews(string itemCat)
        {
            string get = string.Format("SELECT 编号,标题,内容,UBB代码,分类 FROM View_News WHERE 分类='{0}'", itemCat);
            using (DBase db = new DBase())
            {
                return db.GetDataTable(get);
            }
        }

        /// <summary>
        /// 添加新闻
        /// </summary>
        /// <param name="news">新闻数据结构</param>
        public void AddNews(News news)
        {
            if (news.title.Length > 45) news.title = news.title.Substring(0, 45);
            if (news.html.Length > 5000) news.html = news.html.Substring(0, 5000);
            if (news.ubb.Length > 5000) news.ubb = news.ubb.Substring(0, 5000);
            news.title = SpltString(news.title);
            news.html = SpltString(news.html);
            news.ubb = SpltString(news.ubb);
            string ins = string.Format("INSERT INTO View_News(标题,内容,UBB代码,分类)VALUES('{0}','{1}','{2}','{3}')",
                news.title,
                news.html,
                news.ubb,
                news.cat);
            using (DBase db = new DBase())
            {
                db.GetDataTable(ins);
            }
        }

        /// <summary>
        /// 删除新闻
        /// </summary>
        /// <param name="id">编号</param>
        public void DelNews(string id)
        {
            string del = string.Format("DELETE FROM View_News WHERE 编号={0}", id);
            using (DBase db = new DBase())
            {
                db.GetDataTable(del);
            }
        }
        #endregion

        #region 注册用户操作
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="shop">店铺地址</param>
        /// <param name="user">用户名</param>
        /// <param name="psd">密码</param>
        /// <param name="rem">备注</param>
        public void ModifyUser(string id, string shop, string user, string psd, string rem)
        {
            rem = SpltString(rem);
            string modify = string.Format("UPDATE View_User SET 用户名='{0}',密码='{1}',店铺连接='{2}',备注='{3}' Where 编号={4}", user, psd, shop, rem, id);
            using (DBase db = new DBase())
            {
                db.GetDataTable(modify);
            }
        }

        /// <summary>
        /// 获取本月已注册的全部用户
        /// </summary>
        /// <returns>查询结果</returns>
        public DataTable GetAllRegisterUser()
        {
            string get = "SELECT 编号,标题,网址,用户名,密码,店铺连接,备注 From View_RegisterUser";
            using (DBase db = new DBase())
            {
                return db.GetDataTable(get);
            }
        }

        /// <summary>
        /// 查询指定分类ID用户
        /// </summary>
        /// <param name="cid">分类编号</param>
        /// <returns>查询结果</returns>
        public DataTable GetItemCatUser(string cid)
        {
            string get = string.Format("SELECT * FROM View_User WHERE 网址编号={0}", cid);
            using (DBase db = new DBase())
            {
                return db.GetDataTable(get);
            }
        }

        /// <summary>
        /// 获取注册用户信息
        /// </summary>
        /// <returns>查询结果</returns>
        public DataTable GetUser()
        {
            string get = "SELECT * FROM View_User";
            using (DBase db = new DBase())
            {
                return db.GetDataTable(get);
            }
        }
        
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="sid">归属网站ID</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="shop">店铺地址</param>
        /// <param name="remark">备注</param>
        public void AddUser(string sid,string username,string password,string shop,string remark)
        {
            remark = SpltString(remark);
            string ins = string.Format("insert into View_User(用户名,网址编号,密码,店铺连接,备注)values('{0}',{1},'{2}','{3}','{4}')",
                username, sid, password, shop, remark);
            using (DBase db = new DBase())
            {
                db.GetDataTable(ins);
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">编号</param>
        public void DelUser(string id)
        {
            string del = string.Format("DELETE FROM RegUser WHERE id={0}", id);
            DBase db = new DBase();
            db.GetDataTable(del);
        }
        #endregion

        #region 网站基本操作

        /// <summary>
        /// 设置网站已被注册的标志
        /// </summary>
        /// <param name="sid">网址编号</param>
        public void SetIsRegLable(string sid)
        {
            string up = string.Format("UPDATE View_Site SET 是否注册=true WHERE 编号={0}", sid);
            using (DBase db = new DBase())
            {
                db.GetDataTable(up);
            }
        }

        /// <summary>
        /// 获取全部网站
        /// </summary>
        /// <returns>查询结果</returns>
        public DataTable GetSite()
        {
            string get = "SELECT * FROM View_AllSite";
            DBase db = new DBase();
            return db.GetDataTable(get);
        }

        /// <summary>
        /// 获取除黑名单之外的网站
        /// </summary>
        /// <returns>查询结果</returns>
        public DataTable GetSiteNot()
        {
            string get = "SELECT * FROM View_Site";
            DBase db = new DBase();
            return db.GetDataTable(get);
        }

        /// <summary>
        /// 获取指定Url网站
        /// </summary>
        /// <param name="url">Url地址</param>
        /// <returns>查询结果</returns>
        public DataTable GetUrlSite(string url)
        {
            string get = string.Format("SELECT * FROM View_AllSite Where 地址 like('%{0}%')", url);
            using (DBase db = new DBase())
            {
                return db.GetDataTable(get);
            }
        }

        /// <summary>
        /// 获取指定ID网站
        /// </summary>
        /// <param name="id">网站ID</param>
        /// <returns>查询结果</returns>
        public DataTable GetIDSite(string id)
        {
            string get = string.Format("SELECT 编号,地址,标题,创建时间,备注,是否注册,黑名单,成功 FROM View_AllSite Where 编号={0}", id);
            using (DBase db = new DBase())
            {
                return db.GetDataTable(get);
            }
        }

        public void AddSite(string url, string title, string remark)
        {
            title = SpltString(title);
            remark = SpltString(remark);
            string ins = string.Format("INSERT INTO View_Site(地址,标题,备注)VALUES('{0}','{1}','{2}')",
                url, title, remark);
            DBase db = new DBase();
            db.GetDataTable(ins);
        }

        /// <summary>
        /// 删除网站
        /// </summary>
        /// <param name="id">编号</param>
        public void DelSite(string id)
        {
            string del = string.Format("DELETE FROM View_Site WHERE 编号={0}", id);
            DBase db = new DBase();
            db.GetDataTable(del);
        }

        /// <summary>
        /// 修改制定ID网站
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="title">标题</param>
        /// <param name="url">网址</param>
        /// <param name="remark">备注</param>
        /// <param name="is_reg">是否注册true false</param>
        /// <param name="is_del">黑名单true false</param>
        /// <param name="is_sucess">是否成功true false</param>
        public void ModiftSite(
            string id,
            string title,
            string url,
            string remark,
            string is_reg,
            string is_del,
            string is_sucess)
        {
            remark = SpltString(remark);
            if (is_del != "true") is_del = "false";
            if (is_reg != "true") is_reg = "false";
            if (is_sucess != "true") is_sucess = "false";
            string up = string.Format("update Site set url='{0}',title='{1}',remark='{2}',is_reg={3},is_del={4},is_sucess={5} where id={6}", url, title, remark, is_reg, is_del, is_sucess, id);

            using (DBase db = new DBase())
            {
                db.GetDataTable(up);
            }
        }
        #endregion

        #region 基础定义
        /// <summary>
        /// 将字符串中的单引号和双引号转换成中文状态
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <returns>转换后的字符串</returns>
        private string SpltString(string source)
        {
            //IEnumerator operandEnum = source.GetEnumerator();
            //int charCount = 0;
            //int stringCount = 0;
            //while (operandEnum.MoveNext())
            //{
            //    switch (operandEnum.Current.ToString())
            //    {
            //        case "\"":
            //            charCount++;
            //            break;
            //        case "\'":
            //            stringCount++;
            //            break;
            //    }
            //}
            //if (charCount % 2 != 0)
            //{
            //    source = source.Insert(0, "\"");
            //}
            //if (stringCount % 2 != 0)
            //{
            //    source = source.Insert(0, "\'");
            //}
            source = source.Trim();
            source = source.Replace("\'", "\"");
            return source;
        }
        /// <summary>
        /// 用户数据结构
        /// </summary>
        public struct UserList
        {
            public string username;
            public string password;
        }
        #endregion

        public void Dispose()
        {
        }
    }
}